package com.yeyks.controller;

import com.yeyks.dal.dao.MessageMapper;
import com.yeyks.dal.domain.AccountInfo;
import com.yeyks.dal.domain.Message;
import com.yeyks.exception.PermissionInvalidException;
import com.yeyks.exception.TokenInvalidException;
import com.yeyks.model.Ajax.AjaxResponseBody;
import com.yeyks.model.OrderStatus;
import com.yeyks.model.PreOrderInfo;
import com.yeyks.model.em.AccountType;
import com.yeyks.model.em.ResponseStatus;
import com.yeyks.service.AccountService;
import com.yeyks.exception.PermissionInvalidException;
import com.yeyks.exception.TokenInvalidException;
import com.yeyks.model.Ajax.AjaxResponseBody;
import com.yeyks.model.em.AccountType;
import com.yeyks.model.em.ResponseStatus;
import com.yeyks.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.security.InvalidParameterException;
import java.util.List;

@RestController
@RequestMapping(value = "/ajax/msg")
public class MessageController {

    @Autowired
    AccountService accountService;

    @Autowired
    MessageMapper messageMapper;

    /**
     * 查看入住通知消息
     * @param request
     * @param response
     * @param token
     * @param pageNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    @ApiIgnore
    @RequestMapping(value = "/list", method = {RequestMethod.POST, RequestMethod.GET})
    public ResponseEntity<?> list(
            HttpServletRequest request, HttpServletResponse response,
            @RequestParam(name = "token", defaultValue = "", required = true) String token,
            @RequestParam(name = "pageNum", defaultValue = "0", required = true) Integer pageNum,
            @RequestParam(name = "pageSize", defaultValue = "0", required = true) Integer pageSize
    ) throws Exception {
        Integer offset = null, maxrow = null;
        if (pageNum != 0 && pageSize != 0) {
            offset = (pageNum - 1) * pageSize;
            maxrow = pageSize;
        }

        AjaxResponseBody responseBody = new AjaxResponseBody();

        try {
            AccountInfo accountInfo = accountService.token2AccountInfo(token);

            List<Message> messageList = null;
            Integer count = null;
            if (accountInfo.getType() == AccountType.User) {
                throw new PermissionInvalidException();
            } else if (accountInfo.getType() == AccountType.HotelManager) {
                messageList = messageMapper.selectByUserTypePage(accountInfo.getId(), "H", offset, maxrow);
                count = messageMapper.selectByUserTypePageCount(accountInfo.getId(), "H");
            } else if (accountInfo.getType() == AccountType.CityManager) {
                messageList = messageMapper.selectByUserTypePage(accountInfo.getId(), "C", offset, maxrow);
                count = messageMapper.selectByUserTypePageCount(accountInfo.getId(), "C");
            } else if (accountInfo.getType() == AccountType.BranchManager) {
                messageList = messageMapper.selectByUserTypePage(null, "B", offset, maxrow);
                count = messageMapper.selectByUserTypePageCount(null, "B");
            } else if (accountInfo.getType() == AccountType.Administrator) {
                messageList = messageMapper.selectByUserTypePage(null, "A", offset, maxrow);
                count = messageMapper.selectByUserTypePageCount(null, "A");
            } else {

            }

            responseBody.addMapBody("count", count);
            responseBody.addMapBody("list", messageList);

            responseBody.setCode(ResponseStatus.RS_SUCCESS.getCode());
            responseBody.setMsg(ResponseStatus.RS_SUCCESS.getMsg());
        } catch (TokenInvalidException e) {
            responseBody.setCode(ResponseStatus.RS_FAIL_INVALID_TOKEN.getCode());
            responseBody.setMsg(ResponseStatus.RS_FAIL_INVALID_TOKEN.getMsg());
        } catch (PermissionInvalidException e) {
            responseBody.setCode(ResponseStatus.RS_FAIL_INVALID_PERMISSION.getCode());
            responseBody.setMsg(ResponseStatus.RS_FAIL_INVALID_PERMISSION.getMsg());
        } catch (InvalidParameterException e) {
            responseBody.setCode(ResponseStatus.RS_FAIL_INVALID_PERMISSION.getCode());
            responseBody.setMsg(e.getMessage());
        } catch (Throwable e) {
            responseBody.setCode(ResponseStatus.RS_FAIL.getCode());
            responseBody.setMsg(ResponseStatus.RS_FAIL.getMsg());
        }

        return ResponseEntity.ok(responseBody);

    }
}